package lib.gson;

import com.google.gson.Gson;
import org.junit.Assert;
import org.junit.Test;

import java.util.HashMap;

public class QuickStart {
    User user = new User(1, "zing", "123456");
    Gson gson = new Gson();
    String json = "{\"id\":1,\"name\":\"zing\",\"password\":\"123456\"}";

    @Test
    public void toJsonObject() {
        String s = gson.toJson(user);
        Assert.assertEquals(s, json);
        HashMap<String, Object> map = new HashMap<>();
        map.put("id", 1);
        map.put("name", "zing");
        map.put("password", "123456");
        s = gson.toJson(map);
        System.out.println(s);
    }

    @Test
    public void fromJson() {
        HashMap m = gson.fromJson(json, HashMap.class);
        Assert.assertEquals(m.get("name"), "zing");
        Assert.assertEquals((int) (double) m.get("id"), 1);
    }
}
